package com.cskaoyan.controller;

import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.service.AdminKeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: SSMItem
 * @description 该controller为 ‘关键词’ 模块功能的实现
 * @author: YY
 * @create: 2022-10-12 01:39
 **/
@RestController
@RequestMapping("admin/keyword")
public class AdminKeywordController {

    @Autowired
    AdminKeywordService adminKeywordService;

    /**
     * 关键词管理
     * 请求url：http://localhost：8083/admin/keyword/list
     *
     * @param baseParam
     * @return
     */
    @RequiresPermissions("admin:keyword:list")
    @GetMapping("list")
    public BaseRespVo keywordList(BaseParam baseParam, String keyword, String url) {


        //分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        List<MarketKeyword> marketKeywordList = adminKeywordService.selectKeyword(baseParam,keyword,url);

        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(marketKeywordList);

        CommonData data = CommonData.data(pageInfo);

        return BaseRespVo.ok(data);

    }


    /**
     * 添加关键词
     * 请求url：http://localhost：8083/admin/keyword/create
     *
     * @param keyword
     * @return
     */
    @RequiresPermissions("admin:keyword:create")
    @PostMapping("create")
    public BaseRespVo createKeyword(@RequestBody MarketKeyword keyword) {

        //先判断添加的通用问题是否重复

        String keyword1 = adminKeywordService.selectIssueByQuestion(keyword.getKeyword());
        if (keyword1 != null) {
            return BaseRespVo.issue("该关键词已存在，添加失败！");
        }



        MarketKeyword marketKeyword = adminKeywordService.createKeyword(keyword);

        return BaseRespVo.ok(marketKeyword);

    }

    /**
     * 修改关键词
     * 请求url：http://localhost：8083/admin/keyword/update
     *
     * @param keyword
     * @return
     */
    @RequiresPermissions("admin:keyword:update")
    @PostMapping("update")
    public BaseRespVo updateKeyword(@RequestBody MarketKeyword keyword) {

        MarketKeyword marketKeyword = adminKeywordService.updateKeyword(keyword);

        return BaseRespVo.ok(marketKeyword);

    }

    /**
     * 删除关键词
     * 请求url：http://localhost：8083/admin/keyword/delete
     *
     * @param keyword
     * @return
     */
    @RequiresPermissions("admin:keyword:delete")
    @PostMapping("delete")
    public BaseRespVo deleteKeyword(@RequestBody MarketKeyword keyword) {

            int rows = adminKeywordService.deleteKeyword(keyword);
        if (rows == 1) {
            return BaseRespVo.ok(null);
        }
        return BaseRespVo.invalidData("删除失败");

    }
    //TODO;可能有一个显示专题的api GET /admin/keyword/read


}
